//
//  SEGExpressCollectionDetailViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 14-1-2.
//  Copyright (c) 2014年 陈区. All rights reserved.
//

#import "SEGExpressCollectionDetailViewController.h"
#import "SEGQRCodeGeneratorViewController.h"
#import "SEGExpressDetailTableViewCell.h"

#import "SEGExpressCollectionDetailViewModel.h"

@interface SEGExpressCollectionDetailViewController () <UITableViewDelegate, UITableViewDataSource, MFMessageComposeViewControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;
/// viewModel
@property (nonatomic, strong) SEGExpressCollectionDetailViewModel *viewModel;

@end

@implementation SEGExpressCollectionDetailViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self showCustomNavigationBackButton];
    [self showCustomNavigationRightButtonWithTitle:@"二维码" image:nil hightlightImage:nil color:kColor_T];
    
    @weakify(self);
    [[self.viewModel.command execute:self.expressID] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedFailureReason];
    }];
}

#pragma mark - UI
- (void)setupUI {
    self.title = @"详情";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    // 发短信找人带领
    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 70)];
    UIButton* finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    finishBtn.frame = CGRectMake(10, 12, [UIScreen mainScreen].bounds.size.width - 20, 45); //300
    finishBtn.layer.cornerRadius = 4;
    finishBtn.layer.masksToBounds = YES;
    [finishBtn setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [finishBtn setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
    finishBtn.titleLabel.textColor = kColor_2;
    [finishBtn setTitle:NSLocalizedString(@"发短信找人代领", nil) forState:UIControlStateNormal];
    [finishBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [footerView addSubview:finishBtn];
    [[finishBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIButton *btn) {
        NSString* smsStr = [NSString stringWithFormat:@"收件人:%@,电话:%@,提取码:%@,地址:%@", self.viewModel.expressModel.expressReceiveName, self.viewModel.expressModel.expressReceiveTel, self.viewModel.expressModel.receiveKey, self.viewModel.expressModel.expressReceiveAddress];
        
        if (self.viewModel.expressModel.assignedCode.length > 0) {
            smsStr = [smsStr stringByAppendingString:[NSString stringWithFormat:@",柜号:%@", self.viewModel.expressModel.assignedCode]];
        }

        MFMessageComposeViewController* messageComposer;
        if ([MFMessageComposeViewController canSendText]) {
            messageComposer = [[MFMessageComposeViewController alloc] init];
            messageComposer.messageComposeDelegate = self;
            [messageComposer setBody:smsStr]; // 设置发送内容
            messageComposer.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentViewController:messageComposer animated:NO completion:Nil];
        }
    }];
    self.tableView.tableFooterView = footerView;
}

#pragma mark - Event
- (void)onNavigationRightButtonClicked:(UIButton *)sender {
    SEGQRCodeGeneratorViewController* QRCodeGeneratorView = [[SEGQRCodeGeneratorViewController alloc] init];
    QRCodeGeneratorView.hidesBottomBarWhenPushed = YES;
    [QRCodeGeneratorView receiveCode:self.viewModel.expressModel.qrCodeStr];
    [self.navigationController pushViewController:QRCodeGeneratorView animated:YES];
}

#pragma mark - <MFMessageComposeViewControllerDelegate>
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    [self dismissViewControllerAnimated:YES completion:nil];
    if (result == MessageComposeResultSent) {
    }
    else if (result == MessageComposeResultCancelled) {
    }
    else if (result == MessageComposeResultFailed) {
    }
}

#pragma mark - <UITableViewDelegate UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.rowNames.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionData = self.viewModel.rowNames[section];
    return sectionData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGExpressDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SEGExpressDetailTableViewCell" forIndexPath:indexPath];
    NSArray *nameSectionData = self.viewModel.rowNames[indexPath.section];
    NSArray *valueSectionData = self.viewModel.dataSource[indexPath.section];
    cell.nameLabel.text = nameSectionData[indexPath.row];
    cell.valueLabel.text = valueSectionData[indexPath.row];
    cell.valueLabel.textColor = indexPath.section == 0 ? kColor_T : kColor_1;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    }
    else {
        return 20;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.estimatedRowHeight = 44.0f;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerClass:[SEGExpressDetailTableViewCell class] forCellReuseIdentifier:@"SEGExpressDetailTableViewCell"];
    }
    return _tableView;
}
- (SEGExpressCollectionDetailViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGExpressCollectionDetailViewModel alloc] init];
    return _viewModel;
}

@end
